package com.example.sup.conttroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sup.conttroller.request.EquipmentInfoDeleteRequest;
import com.example.sup.conttroller.request.EquipmentInfoRequest;
import com.example.sup.conttroller.response.ServerResponse;
import com.example.sup.entity.EquipmentInfo;
import com.example.sup.service.EquipmentInfoService;
import com.example.sup.vo.EquipmentInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName EquipmentInfoController
 * @Description {TODO}
 * @Author 李哲
 * @Date 19:37 2024/7/23
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/equipmentInfo")
public class EquipmentInfoController {

    @Resource
    private EquipmentInfoService equipmentInfoService;

    @PostMapping("/queryType")
    public ServerResponse query(@RequestBody EquipmentInfoRequest equipmentInfoRequest){
        //1.检验参数
        //2.调用service
        QueryWrapper<EquipmentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipment_type",equipmentInfoRequest.getEquipmentType());
        List<EquipmentInfo> equipmentInfo = equipmentInfoService.list(queryWrapper);
        //3.返回结果
        if (equipmentInfo.size()==0){
            return ServerResponse.fail("没有查询到数据");
        }
        return ServerResponse.successWithMsg("操作成功",equipmentInfo);
    }

    @PostMapping("/queryAll")
    public ServerResponse queryAll(@RequestBody EquipmentInfoRequest equipmentInfoRequest){
        //1.检验参数
        //2.调用service
        Page<EquipmentInfo> page = new Page<>(equipmentInfoRequest.getCurrent(),equipmentInfoRequest.getSize());
        Page<EquipmentInfo> page1 = equipmentInfoService.page(page);
        page1.setTotal( equipmentInfoService.count());
        page1.setPages(equipmentInfoService.count()/equipmentInfoRequest.getSize());
        //3.返回结果
        if (page1.getTotal()==0){
            return ServerResponse.fail("没有查询到数据");
        }
        return ServerResponse.successWithMsg("操作成功",page1);
    }

    @PostMapping("/delete")
    public ServerResponse delete(@RequestBody EquipmentInfoDeleteRequest request){
        //1.检验参数
        //2.调用service
        QueryWrapper<EquipmentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipmentInfo_id",request.getEquipmentInfoId());
        boolean remove = equipmentInfoService.remove(queryWrapper);
        //3.返回结果
        if (!remove){
            return ServerResponse.failWithMsg("操作失败");
        }
        return ServerResponse.successWithMsg("操作成功",null);
    }
}
